package com.xiaoshuidi.cloud.module.rooms.api;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.framework.tenant.core.context.TenantContextHolder;
import com.xiaoshuidi.cloud.module.rooms.api.apartment.ApartmentApi;
import com.xiaoshuidi.cloud.module.rooms.api.apartment.dto.ApartmentListDto;
import com.xiaoshuidi.cloud.module.rooms.api.apartment.vo.ApartmentListRespVO;
import com.xiaoshuidi.cloud.module.rooms.api.apartment.vo.GetApartmentVO;
import com.xiaoshuidi.cloud.module.rooms.api.apartment.vo.RecommendApartmentInfoRespVO;
import com.xiaoshuidi.cloud.module.rooms.convert.apartment.ApartmentConvert;
import com.xiaoshuidi.cloud.module.rooms.pojo.Apartment;
import com.xiaoshuidi.cloud.module.rooms.repository.ApartmentRepository;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.apartment.vo.ApartmentVO;
import com.xiaoshuidi.cloud.module.rooms.convert.apartment.ApartmentConvert;
import com.xiaoshuidi.cloud.module.rooms.service.ApartmentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.xiaoshuidi.cloud.module.system.enums.ApiConstants.VERSION;

/**
 * @Author Feng
 * @date 2023/5/23
 */
@Slf4j
@RestController
@DubboService(version = VERSION)
@Validated
@RequiredArgsConstructor
public class ApartmentApiImpl implements ApartmentApi {

    private final ApartmentService apartmentService;
    private final ApartmentRepository apartmentRepository;
    @Override
    public CommonResult<Long> create(Long deptId, String apartmentName) {
        Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
        return apartmentService.create(deptId,apartmentName,tenantId);
    }

    @Override
    public CommonResult<List<ApartmentListDto>> listByIds(List<Long> ids) {
        return CommonResult.success(apartmentService.listByIds(ids));
    }

    @Override
    public CommonResult<ApartmentListDto> getById(Long id) {
        return CommonResult.success(apartmentService.getById(id));
    }

    @Override
    public CommonResult<Boolean> deleteByDeptId(Long deptId) {
        log.info("删除门店api接口被调用 deptId:{}", deptId);
        return apartmentService.deleteByDeptId(deptId);
    }

    @Override
    public CommonResult<Long> editApartmentName(Long deptId, String name) {
        return apartmentService.editApartmentName(deptId, name);
    }

    @Override
    public CommonResult<List<RecommendApartmentInfoRespVO>> recommendApartmentInfo(List<Long> apartmentIds, String lat, String lon) {
        return apartmentService.recommendApartmentInfo(apartmentIds, lat, lon);
    }

    @Override
    public CommonResult<List<RecommendApartmentInfoRespVO>> recommendApartmentInfoByName(String apartmentName) {
        return apartmentService.recommendApartmentInfoByName(apartmentName);
    }

    @Override
    public CommonResult<List<ApartmentListRespVO>> apartmentAllList() {
        List<Apartment> apartmentList = apartmentRepository.list();
        return CommonResult.success(ApartmentConvert.INSTANCE.convert1(apartmentList));
    }

    @Override
    public CommonResult<ApartmentListDto> queryApartmentByName(String apartmentName) {
        return CommonResult.success(ApartmentConvert.INSTANCE.convertToApartmentListDto(apartmentService.queryApartmentByName(apartmentName)));
    }

    /**
     * 获取全部门店信息
     * @return
     */
    @Override
    public CommonResult<List<GetApartmentVO>> getAllApartment() {
        log.info("[获取全部门店信息Api接口被调用]--tenantId:{}", TenantContextHolder.getTenantId());
        List<ApartmentVO> checkedData = apartmentService.myList(null, null).getCheckedData();
        log.info("[获取全部门店信息Api接口被调用]--tenantId:{}, 门店数据:{}", TenantContextHolder.getTenantId(), JsonUtils.toJsonString(checkedData));
        List<GetApartmentVO> getApartmentVOS = ApartmentConvert.INSTANCE.convert2(checkedData);
        return CommonResult.success(getApartmentVOS);
    }
}
